{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "lLGpv1fLLIjF"
   },
   "source": [
    "# Tracing\n",
    "\n",
    "In particular, we provide two tracing methods to help you develop and improve the Generator:\n",
    "\n",
    "1. Trace the history change(states) on prompt during your development process. Developers typically go through a long process of prompt optimization and it is frustrating to lose track of the prompt changes when your current change actually makes the performance much worse.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "sfKEfaYC3Go7"
   },
   "outputs": [],
   "source": [
    "from IPython.display import clear_output\n",
    "\n",
    "!pip install -U adalflow[openai,groq,faiss-cpu]\n",
    "\n",
    "clear_output()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip uninstall httpx anyio -y\n",
    "!pip install \"anyio>=3.1.0,<4.0\"\n",
    "!pip install httpx==0.24.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "-4c_AGBt3PlR",
    "outputId": "85aba038-ee9c-463d-bdbd-027cbfff0094"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter your OpenAI API key: ··········\n",
      "Please enter your GROQ API key: ··········\n",
      "API keys have been set.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "\n",
    "# Prompt user to enter their API keys securely\n",
    "openai_api_key = getpass(\"Please enter your OpenAI API key: \")\n",
    "groq_api_key = getpass(\"Please enter your GROQ API key: \")\n",
    "\n",
    "# Set environment variables\n",
    "os.environ[\"OPENAI_API_KEY\"] = openai_api_key\n",
    "os.environ[\"GROQ_API_KEY\"] = groq_api_key\n",
    "\n",
    "print(\"API keys have been set.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "yWi2uEiE6UIf"
   },
   "source": [
    "We created a GeneratorStateLogger to handle the logging and saving into json files. To further simplify developers’s process, we provides a class decorator trace_generator_states where a single line of code can be added to any of your task component. It will automatically track any attributes of type Generator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "id": "qk9pkcCVzdek"
   },
   "outputs": [],
   "source": [
    "from adalflow.tracing import trace_generator_states\n",
    "from adalflow.core import Component, Generator\n",
    "import adalflow as adal\n",
    "from adalflow.components.model_client import OpenAIClient\n",
    "\n",
    "template_doc = r\"\"\"<SYS> You are a doctor </SYS> User: {{input_str}}\"\"\"\n",
    "\n",
    "\n",
    "@trace_generator_states()\n",
    "class DocQA(adal.Component):\n",
    "    def __init__(self):\n",
    "        super(DocQA, self).__init__()\n",
    "        self.generator = Generator(\n",
    "            template=template_doc,\n",
    "            model_client=OpenAIClient(),\n",
    "            model_kwargs={\"model\": \"gpt-4o-mini\"},\n",
    "        )\n",
    "\n",
    "    def call(self, query: str) -> str:\n",
    "        return self.doc(prompt_kwargs={\"input_str\": query}).data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "LAZUSnYn-lnI"
   },
   "source": [
    "Here is the folder structer of where the trace is generated as a .json file and also an example output below"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "cVofNXVW-EMo"
   },
   "source": [
    "![image.png]()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "dPd9i6_t7ERJ"
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    " {\n",
    "    \"doc\": [\n",
    "        {\n",
    "            \"prompt_states\": {\n",
    "                \"type\": \"Prompt\",\n",
    "                \"data\": {\n",
    "                    \"_components\": {\n",
    "                        \"_ordered_dict\": true,\n",
    "                        \"data\": []\n",
    "                    },\n",
    "                    \"_parameters\": {\n",
    "                        \"_ordered_dict\": true,\n",
    "                        \"data\": []\n",
    "                    },\n",
    "                    \"training\": false,\n",
    "                    \"teacher_mode\": false,\n",
    "                    \"tracing\": false,\n",
    "                    \"name\": \"Prompt\",\n",
    "                    \"_init_args\": {\n",
    "                        \"template\": null,\n",
    "                        \"prompt_kwargs\": {}\n",
    "                    },\n",
    "                    \"template\": \"<SYS> You are a doctor </SYS> User: {{input_str}}\",\n",
    "                    \"prompt_variables\": [\n",
    "                        \"input_str\"\n",
    "                    ],\n",
    "                    \"prompt_kwargs\": {}\n",
    "                }\n",
    "            },\n",
    "            \"time_stamp\": \"2024-11-29T12:36:33.302956\"\n",
    "        }\n",
    "    ]\n",
    "}\n",
    "\"\"\""
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
